package com.huaxin.dss2.data.dao;

import com.huaxin.dss2.Launcher;
import com.huaxin.dss2.data.bean.properties.HxDssProperties;
import com.huaxin.dss2.data.dao.base.BaseDataDao;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

/**
 * dataDao工厂类，通过配置方式选择dataDao
 * mysql : MySQLDataDao {@link MySQLDataDao}
 * hbase : HBaseDataDao {@link HBaseDataDao}
 * oracle : oracle {@link OracleDataDao}
 * @author : luqing
 * @date : 2018/6/27 16:18
 */

@Component
@DependsOn("propertiesPersister")
@Slf4j
public class DataDaoFactory implements ApplicationContextAware {


    private static ApplicationContext applicationContext;
    private static HxDssProperties properties;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        DataDaoFactory.applicationContext = applicationContext;
    }

    @Autowired
    public void setProperties(HxDssProperties properties) {
        DataDaoFactory.properties = properties;
    }

    /**
     * @return 数据源dataDao
     */
    public static BaseDataDao createDataDao() throws Exception {
        Map<String, BaseDataDao> beansOfType = applicationContext.getBeansOfType(BaseDataDao.class);
        BaseDataDao baseDataDao = beansOfType.get(properties.getDataType()+"DataDao");
        if (baseDataDao == null){
            log.error("请配置正确的历史数据数据源:{}",beansOfType.keySet());
            Launcher.shutdown();
        }
        return baseDataDao;
    }
}
